package com.ruoyi.web.controller.system;

import java.util.*;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.JsonResult;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.InfoCategoryDept;
import com.ruoyi.system.service.IInfoCategoryDeptService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * infocategorydeptController
 * 
 * @author ruoyi
 * @date 2023-04-07
 */
@Controller
@RequestMapping("/system/infocategorydept")
public class InfoCategoryDeptController extends BaseController
{
    private String prefix = "system/infocategorydept";

    @Autowired
    private IInfoCategoryDeptService infoCategoryDeptService;

    @Autowired
    private ISysDeptService sysDeptService;

    @PreAuthorize("@ss.hasPermi('system:infocategorydept:view')")
    @GetMapping()
    public String dept()
    {
        return prefix + "/infocategorydept";
    }

    /**
     * 查询infocategorydept列表
     */
    @PreAuthorize("@ss.hasPermi('system:infocategorydept:list')")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InfoCategoryDept infoCategoryDept)
    {
        startPage();
        List<InfoCategoryDept> list = infoCategoryDeptService.selectInfoCategoryDeptList(infoCategoryDept);
        return getDataTable(list);
    }

    @PostMapping("find_dept_by_categoryid")
    @ResponseBody
    public JsonResult<?> findDeptByCategoryId(@RequestParam("categoryid") String ppCategoryId) {
        try {
            InfoCategoryDept infoCategoryDept = new InfoCategoryDept();
            infoCategoryDept.setCategoryid(ppCategoryId);
            List<InfoCategoryDept> mmList = infoCategoryDeptService.selectInfoCategoryDeptList(infoCategoryDept);

            List<Map<String,Object>> mmReturnList = new ArrayList<>();
            for (int i = 0; i < mmList.size(); i++) {
                Long deptid = new Long(mmList.get(i).getDeptid());
                SysDept sysdept = sysDeptService.selectDeptById(deptid);

                Map<String,Object> returnMap = new HashMap<>();
                returnMap.put("deptId",sysdept.getDeptId());
                returnMap.put("deptName",sysdept.getDeptName());
                returnMap.put("id",mmList.get(i).getId());
                mmReturnList.add(returnMap);
            }

            return JsonResult.getSuccessResult(mmReturnList);
        } catch (Exception e) {
            logger.error("infocategorydept/find_dept_by_categoryid错误", e);
            return JsonResult.getErrorResult("infocategorydept/find_dept_by_categoryid错误");
        }
    }

    //获取未授权的单位
    @PostMapping("find_wsqdept_categoryid")
    @ResponseBody
    public JsonResult<?>findWSQDeptByCategoryId(@RequestParam("categoryid")String ppCategoryId){
        try {
            //有效单位列表
            SysDept sysDept = new SysDept();
            sysDept.setStatus("0");
            List<SysDept> mmDeptList = sysDeptService.selectDeptList(sysDept);

            InfoCategoryDept infoCategoryDept = new InfoCategoryDept();
            infoCategoryDept.setCategoryid(ppCategoryId);
            //当前栏目管理单位列表
            List<InfoCategoryDept> mmCategoryDeptList = infoCategoryDeptService.selectInfoCategoryDeptList(infoCategoryDept);
            for(int i=0;i<mmCategoryDeptList.size();i++) {
                String mmDeptId = mmCategoryDeptList.get(i).getDeptid() == null ?"":mmCategoryDeptList.get(i).getDeptid();
                for(int j=0;j<mmDeptList.size();j++) {
                    if ((mmDeptList.get(j).getDeptId().toString()).equals(mmDeptId)) {
                        mmDeptList.remove(j);
                    }
                }
            }
            return JsonResult.getSuccessResult(mmDeptList);
        } catch (Exception e) {
            logger.error("infocategorydept/find_wsqdept_categoryid错误", e);
            return JsonResult.getErrorResult("infocategorydept/find_wsqdept_categoryid错误");
        }
    }

    @PostMapping("insert")
    @ResponseBody
    public JsonResult<?> insert(@RequestParam("categoryid") String ppCategoryId, @RequestParam("deptid") String ppDeptId) {
        try {
            InfoCategoryDept infoCategoryDept = new InfoCategoryDept();
            infoCategoryDept.setCategoryid(ppCategoryId);
            infoCategoryDept.setDeptid(ppDeptId);
            List<InfoCategoryDept> mmList = infoCategoryDeptService.selectInfoCategoryDeptList(infoCategoryDept);
            if (mmList.size()>0) {
                return JsonResult.getErrorResult("该管理单位已存在");
            }
            InfoCategoryDept mmInfoCategoryDept = new InfoCategoryDept();
            mmInfoCategoryDept.setId(UUID.randomUUID().toString());
            mmInfoCategoryDept.setCategoryid(ppCategoryId);
            mmInfoCategoryDept.setDeptid(ppDeptId);
            int mmResult = infoCategoryDeptService.insertInfoCategoryDept(mmInfoCategoryDept);
            return mmResult == 1 ? JsonResult.getSuccessResult(mmInfoCategoryDept)
                    : JsonResult.getErrorResult("InfoCategoryDept添加失败");
        } catch (Exception e) {
            logger.error("infocategorydept/insert", e);
            return JsonResult.getErrorResult("infocategorydept/insert错误");
        }
    }

    @PostMapping("delete")
    @ResponseBody
    public JsonResult<?>delete(@RequestParam("id")String ppId){
        try {
            InfoCategoryDept mmInfoCategoryDept = infoCategoryDeptService.selectInfoCategoryDeptById(ppId);
            if (mmInfoCategoryDept==null) {
                return JsonResult.getErrorResult("该栏目单位关系不存在");
            }
            int mmResult = infoCategoryDeptService.deleteInfoCategoryDeptById(ppId);
            return mmResult==1?JsonResult.getSuccessResult("删除成功"):JsonResult.getErrorResult("删除失败");
        } catch (Exception e) {
            logger.error("infocategorydept/delete错误", e);
            return JsonResult.getErrorResult("infocategorydept/delete错误");
        }
    }

    /**
     * 导出infocategorydept列表
     */
    @PreAuthorize("@ss.hasPermi('system:infocategorydept:export')")
    @Log(title = "infocategorydept", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InfoCategoryDept infoCategoryDept)
    {
        List<InfoCategoryDept> list = infoCategoryDeptService.selectInfoCategoryDeptList(infoCategoryDept);
        ExcelUtil<InfoCategoryDept> util = new ExcelUtil<InfoCategoryDept>(InfoCategoryDept.class);
        return util.exportExcel(list, "infocategorydept数据");
    }

    /**
     * 新增infocategorydept
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存infocategorydept
     */
    @PreAuthorize("@ss.hasPermi('system:infocategorydept:add')")
    @Log(title = "infocategorydept", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(InfoCategoryDept infoCategoryDept)
    {
        return toAjax(infoCategoryDeptService.insertInfoCategoryDept(infoCategoryDept));
    }

    /**
     * 修改infocategorydept
     */
    @PreAuthorize("@ss.hasPermi('system:infocategorydept:edit')")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        InfoCategoryDept infoCategoryDept = infoCategoryDeptService.selectInfoCategoryDeptById(id);
        mmap.put("infoCategoryDept", infoCategoryDept);
        return prefix + "/edit";
    }

    /**
     * 修改保存infocategorydept
     */
    @PreAuthorize("@ss.hasPermi('system:infocategorydept:edit')")
    @Log(title = "infocategorydept", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(InfoCategoryDept infoCategoryDept)
    {
        return toAjax(infoCategoryDeptService.updateInfoCategoryDept(infoCategoryDept));
    }

    /**
     * 删除infocategorydept
     */
    @PreAuthorize("@ss.hasPermi('system:infocategorydept:remove')")
    @Log(title = "infocategorydept", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(infoCategoryDeptService.deleteInfoCategoryDeptByIds(ids));
    }
}
